package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.help.menu.InitMenuHelp;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.service.FineClueService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.fineclue.pojo.dto.EnterpriseDetailGetDTO;
import com.xbongbong.pro.fineclue.pojo.dto.FineClueConsumptionDTO;
import com.xbongbong.pro.fineclue.pojo.dto.FineClueCustomerDataDTO;
import com.xbongbong.pro.fineclue.pojo.dto.FineClueGetEnableBuinessDTO;
import com.xbongbong.pro.fineclue.pojo.dto.FineClueGetUseDosageDTO;
import com.xbongbong.pro.fineclue.pojo.dto.FineClueOfficiallyOpenDTO;
import com.xbongbong.pro.fineclue.pojo.dto.FineClueOpenTrialDTO;
import com.xbongbong.pro.fineclue.pojo.dto.FineCluePageGetDTO;
import com.xbongbong.pro.fineclue.pojo.dto.FineClueRedisTypeDTO;
import com.xbongbong.pro.fineclue.pojo.dto.FineClueUpdateDosageDTO;
import com.xbongbong.pro.fineclue.pojo.dto.FineClueUpdateListDTO;
import com.xbongbong.pro.fineclue.pojo.dto.FineClueUserDTO;
import com.xbongbong.pro.fineclue.pojo.dto.FineClueUserSurplusUsageDTO;
import com.xbongbong.pro.fineclue.pojo.vo.EnterpriseDetailGetVO;
import com.xbongbong.pro.fineclue.pojo.vo.FineClueConsumptionVO;
import com.xbongbong.pro.fineclue.pojo.vo.FineClueGetEnableBuinessVO;
import com.xbongbong.pro.fineclue.pojo.vo.FineClueMsgVO;
import com.xbongbong.pro.fineclue.pojo.vo.FineCluePageGetVO;
import com.xbongbong.pro.fineclue.pojo.vo.FineClueUserListVO;
import com.xbongbong.pro.fineclue.pojo.vo.FineClueUserSurplusUsageVO;
import com.xbongbong.pro.menu.pojo.dto.MenuInitDTO;
import com.xbongbong.pro.menu.pojo.vo.MenuInitVO;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @Author mingliang
 * @Description 精线索
 **/
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/fineClue")
public class FineClueController {

    @Resource
    private FineClueService fineClueService;
    @Resource
    private InitMenuHelp initMenuHelp;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    @RequestMapping(value = "/initMenu", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String initSearchCustomer(@RequestBody @Valid MenuInitDTO menuInitDTO, BindingResult br){
        XbbResponse<MenuInitVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                if (!Objects.equals(menuInitDTO.getAlias(), XbbRefTypeEnum.SEARCH_CUSTOMER.getAlias())) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100028);
                }
                MenuInitVO menuInitVO = initMenuHelp.menuInit(menuInitDTO);
                response = new XbbResponse<>(menuInitVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * @Author mingliang
     * @Description 工商查询列表查询接口
     * @Date 2021/10/12 11:30
     * @param enterpriseDetailGetDTO
     * @param br
     **/
    @RequestMapping(value = "/enterprise/getlist", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String enterpriseGetList(@RequestBody @Valid EnterpriseDetailGetDTO enterpriseDetailGetDTO, BindingResult br) {
        XbbResponse<EnterpriseDetailGetVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                EnterpriseDetailGetVO enterpriseDetailGetVO = fineClueService.enterpriseGetList(enterpriseDetailGetDTO);
                response = new XbbResponse<>(enterpriseDetailGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /***
     * @description: 用量查询
     * @param: fineClueConsumptionDTO
     * @param: br
     * @return: java.lang.String
     * @author: zhushuainan
     * @date: 2021/11/4 19:54
     */
    @RequestMapping(value = "/enterprise/getConsumption", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String GetFineClueConsumption(@RequestBody @Valid FineClueConsumptionDTO fineClueConsumptionDTO, BindingResult br){
        XbbResponse<FineClueConsumptionVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FineClueConsumptionVO fineClueConsumptionVO = fineClueService.getFineClueConsumption(fineClueConsumptionDTO);
                response = new XbbResponse<>(fineClueConsumptionVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /***
     * @description: 获取精线索提供的页面
     * @param: fineCluePageGetDTO
     * @param: br
     * @return: java.lang.String
     * @author: zhushuainan
     * @date: 2021/11/9 15:39
     */
    @RequestMapping(value = "/enterprise/getFineCluePage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getFineCluePage(@RequestBody @Valid FineCluePageGetDTO fineCluePageGetDTO, BindingResult br){
        XbbResponse<FineCluePageGetVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            FineCluePageGetVO fineCluePageGetVO = null;
            try {
                fineCluePageGetVO = fineClueService.getFineCluePage(fineCluePageGetDTO);
                response = new XbbResponse<>(fineCluePageGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /***
     * @description: 试用搜客或工商查询
     * @param: fineClueOpenTrialDTO
     * @param: br
     * @return: java.lang.String
     * @author: zhushuainan
     * @date: 2021/11/17 11:22
     */
    @RequestMapping(value = "/enterprise/openTrial", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String openTrial(@RequestBody @Valid FineClueOpenTrialDTO fineClueOpenTrialDTO, BindingResult br){
        XbbResponse<FineClueMsgVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            FineClueMsgVO fineClueMsgVO = null;
            try {
                fineClueMsgVO = fineClueService.openSoukeOrCompanyQueryTrial(fineClueOpenTrialDTO);
                response = new XbbResponse<>(fineClueMsgVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /***
     * @description: 开通正式版
     * @param: fineClueOpenTrialDTO
     * @param: br
     * @return: java.lang.String
     * @author: zhushuainan
     * @date: 2021/12/9 20:02
     */
    @RequestMapping(value = "/enterprise/openOfficialVersion", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String openOfficialVersion(@RequestBody @Valid FineClueOfficiallyOpenDTO fineClueOfficiallyOpenDTO, BindingResult br){
        XbbResponse<FineClueMsgVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            FineClueMsgVO fineClueMsgVO = null;
            try {
                fineClueMsgVO = fineClueService.openOfficialVersion(fineClueOfficiallyOpenDTO);
                response = new XbbResponse<>(fineClueMsgVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /***
     * @description: 用量不够时，点击获取更多用量 ，发送一条销售机会
     * @param: fineClueOfficiallyOpenDTO
     * @param: br
     * @return: java.lang.String
     * @author: zhushuainan
     * @date: 2022/1/10 17:35
     */
    @RequestMapping(value = "/enterprise/getMoreConsumption", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getMoreConsumption(@RequestBody @Valid BaseDTO baseDTO, BindingResult br){
        XbbResponse<FineClueMsgVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            FineClueMsgVO fineClueMsgVO = null;
            try {
                fineClueMsgVO = fineClueService.getMoreConsumption(baseDTO);
                response = new XbbResponse<>(fineClueMsgVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

//    /** *
//     * @description: 线索释放
//     * @param: fineClueOpenTrialDTO
//     * @param: br
//     * @return: java.lang.String
//     * @author: zhushuainan
//     * @date: 2021/11/30 14:13
//     */
//    @RequestMapping(value = "/clue/release", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    public String clueRelease(@RequestBody @Valid FineClueClueReleaseDTO fineClueClueReleaseDTO, BindingResult br){
//        XbbResponse<FineClueMsgVO> response;
//        if (br.hasErrors()){
//            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
//            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
//        } else {
//            FineClueMsgVO fineClueMsgVO = null;
//            try {
//                fineClueMsgVO = fineClueService.releaseClue(fineClueClueReleaseDTO);
//                response = new XbbResponse<>(fineClueMsgVO);
//            } catch (XbbException e) {
//                response = new XbbResponse<>(e);
//            }
//        }
//        return JSON.toJSONString(response);
//    }

    /**
     * @Author mingliang
     * @Description 通过精线索，查询企业详情
     * @Date 2021/10/12 11:30
     * @param enterpriseDetailGetDTO
     * @param br 
     **/
    @RequestMapping(value = "/enterprise/getInfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String enterpriseGetInfo(@RequestBody @Valid EnterpriseDetailGetDTO enterpriseDetailGetDTO, BindingResult br) {
        XbbResponse<EnterpriseDetailGetVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                EnterpriseDetailGetVO enterpriseDetailGetVO = fineClueService.enterpriseGetInfo(enterpriseDetailGetDTO);
                response = new XbbResponse<>(enterpriseDetailGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * @Author mingliang
     * @Description 获得精线索菜单使用权限 从组织架构中获取人员放入坐席列表中
     * @param fineClueUserDTO
     * @param br
     **/
    @RequestMapping(value = "/dep/userList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getDepUserList(@RequestBody @Valid FineClueUserDTO fineClueUserDTO, BindingResult br) {
        FineClueUserListVO fineClueUserListVO;
        XbbResponse<FineClueUserListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                fineClueUserListVO = fineClueService.getDepUserList(fineClueUserDTO);
                response = new XbbResponse<>(fineClueUserListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * @Author mingliang
     * @Description 批量设置坐席
     * @param fineClueUpdateListDTO
     * @param br
     **/
    @RequestMapping(value = "/set/staff", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String updateStaff(@RequestBody @Valid FineClueUpdateListDTO fineClueUpdateListDTO, BindingResult br) {
        FineClueMsgVO fineClueMsgVO;
        XbbResponse<FineClueMsgVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(fineClueUpdateListDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(fineClueUpdateListDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            try {
                fineClueMsgVO = fineClueService.batchUpdateUserStaff(fineClueUpdateListDTO);
                response = new XbbResponse<>(fineClueMsgVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * @Author mingliang
     * @Description 批量设置用量 200/天 || x/月
     * @param fineClueUpdateDosageDTO
     * @param br
     **/
    @RequestMapping(value = "/set/dosage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String batchUpdateUserStaff(@RequestBody @Valid FineClueUpdateDosageDTO fineClueUpdateDosageDTO, BindingResult br) {
        FineClueMsgVO fineClueMsgVO;
        XbbResponse<FineClueMsgVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                fineClueMsgVO = fineClueService.batchUpdateUserDosage(fineClueUpdateDosageDTO);
                response = new XbbResponse<>(fineClueMsgVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

//    /***
//     * @description: 解锁联系方式时在用量表里新增一条记录
//     * @param: clueCustomerDataDTO
// * @param: br
//     * @return: java.lang.String
//     * @author: zhushuainan
//     * @date: 2021/11/22 9:47
//     */
//    @RequestMapping(value = "/add/dosageRecord", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    public String contactInfoUnlockUseDosage(@RequestBody @Valid FineClueUnlockDataDTO clueUnlockDataDTO, BindingResult br) {
//        FineClueMsgVO fineClueMsgVO;
//        XbbResponse<FineClueMsgVO> response;
//        if (br.hasErrors()) {
//            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
//            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
//        } else {
//            try {
//                fineClueMsgVO = fineClueService.addUseDosageRecord(clueUnlockDataDTO);
//                response = new XbbResponse<>(fineClueMsgVO);
//            } catch (XbbException e) {
//                response = new XbbResponse<>(e);
//            }
//        }
//        return JSON.toJSONString(response);
//    }

    /*** 
     * @description: 第三方获取用户剩余用量
     * @param: fineClueUserSurplusUsageDTO
 * @param: br
     * @return: java.lang.String 
     * @author: zhushuainan
     * @date: 2021/12/6 16:58
     */
    @RequestMapping(value = "/get/userSurplusUsage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String userSurplusUsageStatistics(@RequestBody @Valid FineClueUserSurplusUsageDTO fineClueUserSurplusUsageDTO, BindingResult br) {
        FineClueUserSurplusUsageVO fineClueUserSurplusUsageVO;
        XbbResponse<FineClueUserSurplusUsageVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                fineClueUserSurplusUsageVO = fineClueService.getSurplusUsage(fineClueUserSurplusUsageDTO);
                response = new XbbResponse<>(fineClueUserSurplusUsageVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * @Author mingliang
     * @Description 精线索转客户或线索
     * @param clueCustomerDataDTO
     * @param br
     **/
    @RequestMapping(value = "/add/customer", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String transitionCustomerOrClue(@RequestBody @Valid FineClueCustomerDataDTO clueCustomerDataDTO, BindingResult br) {
        FineClueMsgVO fineClueMsgVO;
        XbbResponse<FineClueMsgVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(clueCustomerDataDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(clueCustomerDataDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            try {
                fineClueMsgVO = fineClueService.transitionCustomerOrClue(clueCustomerDataDTO);
                response = new XbbResponse<>(fineClueMsgVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/set/toCustomerInfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String setTransitionInfo(@RequestBody @Valid FineClueRedisTypeDTO fineClueRedisTypeDTO, BindingResult br) {
        FineClueMsgVO fineClueMsgVO;
        XbbResponse<FineClueMsgVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                fineClueMsgVO = fineClueService.setTransitionInfo(fineClueRedisTypeDTO);
                response = new XbbResponse<>(fineClueMsgVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    
    @RequestMapping(value = "/getEnableBusiness", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getEnableBusiness(@RequestBody @Valid FineClueGetEnableBuinessDTO fineClueGetEnableBuinessDTO, BindingResult br) {
        XbbResponse<FineClueGetEnableBuinessVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FineClueGetEnableBuinessVO fineClueMsgVO = fineClueService.getEnableBuiness(fineClueGetEnableBuinessDTO);
                response = new XbbResponse<>(fineClueMsgVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /***
     * @description: 三方推送解锁用量信息
     * @param: fineClueGetUseDosageDTO
 * @param: br
     * @return: java.lang.String
     * @author: zhushuainan
     * @date: 2022/2/14 9:52
     */
    @RequestMapping(value = "/getUseDosage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getUseDosage(@RequestBody @Valid FineClueGetUseDosageDTO fineClueGetUseDosageDTO, BindingResult br) {
        XbbResponse<FineClueMsgVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FineClueMsgVO fineClueMsgVO = fineClueService.getUseDosage(fineClueGetUseDosageDTO);
                response = new XbbResponse<>(fineClueMsgVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

}
